function ChatSocket() {
    var self = this;
    var socket = null;
    var handlers = [];

    this.connect = function(url) {
        // fall back order
        //socket = io.connect(url, { transports: ['websocket',
		//	'flashsocket', 'xhr-polling', 'xhr-multipart', 'jsonp-polling']
        //});
        socket = io.connect(url, { 
            'transports': ['flashsocket', 'xhr-polling', 'jsonp-polling'],
            'reconnection delay': 500,
            'max reconnection attempts': 10
        });	

        // socket event handlers
        socket.on('message', self.onMessage);
        socket.on('disconnect', self.onDisconnect);
        socket.on('connect', self.onConnect);

        // error
        socket.on('error', self.onError);
    }

    this.onError = function (reason) {
        console.error('Unable to connect Socket.IO: ', reason);
    }

    var dispatch = function (data) {
        var funcs = handlers[data.command];
        for (func in funcs) {
            if (funcs.hasOwnProperty(func)) {
                funcs[func](data);
            }
        }
    }

    this.onMessage = function(data) {
        //data = $.evalJSON(data);
        if (typeof data.command == 'undefined') {
            return;
        }

        dispatch(data);
    }

    this.onDisconnect = function () {
        dispatch({ command: 'disconnect' });
    }

    this.onConnect = function () {
        // connect to chat socket OK, if has already logged in do log in against chat socket
        dispatch({ command: 'connection' });
    }

    this.bind = function(name, func) {
        if (typeof handlers[name] == 'undefined') {
            handlers[name] = [];
        }
        handlers[name].push(func);
        return this;
    }

    this.unbind = function (name, func) {
        var funcs = handlers[name];
        if (typeof funcs == 'undefined') {
            return this;
        }

        var index = funcs.indexOf(func);
        if (index != -1) {
            funcs.splice(funcs.indexOf(func), 1);
        }

        return this;
    }

    this.send = function(data) {
        socket.send($.toJSON(data));
        return this;
    }

    return this;
}